import JDBCUtiles.Utils;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

/**
 * @Author 不知名网友鑫
 * @Date 2022/6/26
 *代码实现1：插入一个新的student 信息。
 *代码实现2：输入身份证号或准考证号可以查询到学生的基本信息。
 *代码实现3:删除学生。
 **/
public class Exercise2 {
    public static void main(String[] args) {
        //代码实现1：插入一个新的student 信息
        Scanner in = new Scanner(System.in);
        System.out.println("请输入考生的详细信息：");
        String sql = "insert into examstudent(Type,IDCard,ExamCard,StudentName,Location,Grade)values(?,?,?,?,?,?)";
        System.out.println("Type:");
        int Type = in.nextInt();
        System.out.println("IDCard:");
        String IDCard = in.next();
        System.out.println("ExamCard:");
        String ExamCard = in.next();
        System.out.println("StudentName:");
        String StudentName = in.next();
        System.out.println("Location:");
        String Location = in.next();
        System.out.println("Grade:");
        int Grade = in.nextInt();
        int update = update(sql, Type, IDCard, ExamCard, StudentName, Location, Grade);
        if(update>0){
            System.out.println("插入成功 ");
        }else{
            System.out.println("失败");
        }


        // 代码实现2：输入身份证号或准考证号可以查询到学生的基本信息。
        Scanner in = new Scanner(System.in);
        System.out.println("请输入您要输入的类型：");
        System.out.println("a:准考证号");
        System.out.println("b:身份证号");
        boolean flag = true;
        String sql = null;
        String obj = null;
        ExamStudent select = null;
        while (flag) {
            String str = in.next();
            switch (str) {
                case "a":
                    System.out.println("请输入准考证号");
                    sql = "select * from examstudent where ExamCard = ?";
                    obj = in.next();

                    flag = false;
                    break;
                case "b":
                    System.out.println("请输入身份证号");
                    sql = "select * from examstudent where IDCard = ?";
                    ;
                    obj = in.next();
                    flag = false;
                    break;
                default:
                    System.out.println("输入有误，请重新输入:");
                    break;
            }
        }
        select = select(sql, obj);
        if (select != null) {
            System.out.println(select);
        }else{
            System.out.println("输入号码错误");
        }

        //代码实现3:删除学生
        System.out.println("请输入学生的考号");
        Scanner in = new Scanner(System.in);
        String sql = "delete from examstudent where ExamCard = ?";
        String str = in.next();
        int update = update(sql, str);
        if(update>0){
            System.out.println("操作成功");
        }else{
            System.out.println("操作失败");
        }

    }

    //代码实现1/3
    public static int update(String sql, Object... obj) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = Utils.getConnection();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < obj.length; i++) {
                ps.setObject(i + 1, obj[i]);
            }
            int i = ps.executeUpdate();
            return i;
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            Utils.closeResource(conn, ps);
        }
    }

    // 代码实现2
    public static ExamStudent select(String sql, String str) {

        ExamStudent es = null;
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {

            conn = Utils.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setString(1, str);
            rs = ps.executeQuery();
            //这里一定要有这一行，next()：查询下面一行是否有数据，如果有，将指针下移。
            if(rs.next()) {
                int flowID = rs.getInt("FlowID");
                int Type = rs.getInt("Type");
                String IDCard = rs.getString("IDCard");
                String ExamCard = rs.getString("ExamCard");
                String StudentName = rs.getString("StudentName");
                String Location = rs.getString("Location");
                int Grade = rs.getInt("Grade");
                es = new ExamStudent(flowID, Type, IDCard, ExamCard, StudentName, Location, Grade);
                return es;
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Utils.closeResource(conn,ps,rs);
        }
        return null;
    }
}

